package com.ruoyi.school.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.school.domain.PacReslutLevel;
import com.ruoyi.school.domain.PacReslutLevel2;
import com.ruoyi.school.service.PacReslutLevel2Service;
import com.ruoyi.school.mapper.PacReslutLevel2Mapper;
import com.ruoyi.system.service.ReslutLevelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author jeremylin
* @description 针对表【pac_reslut_level2(竞赛子项)】的数据库操作Service实现
* @createDate 2025-07-24 08:45:34
*/
@Service
public class PacReslutLevel2ServiceImpl extends ServiceImpl<PacReslutLevel2Mapper, PacReslutLevel2>
    implements PacReslutLevel2Service{
    @Resource
    private ReslutLevelService reslutLevelService;

    @Override
    public List<PacReslutLevel2> getList(PacReslutLevel2 rereslutLevel) {
        List<PacReslutLevel2> list = lambdaQuery().eq(PacReslutLevel2::getResLevelId, rereslutLevel.getResLevelId()).list();
        List<Long> ids = list.stream().map(PacReslutLevel2::getResLevelId).collect(Collectors.toList());
        List<PacReslutLevel> pacReslutLevels = reslutLevelService.selectByResultTypeIds(ids);
        Map<Integer, PacReslutLevel> reslutLevelMap = pacReslutLevels.stream().collect(Collectors.toMap(PacReslutLevel::getId, pacReslutLevel -> pacReslutLevel));
        list.forEach(item -> {
            item.setResLevel(reslutLevelMap.get(item.getResLevelId().intValue()).getResName());
        });
        return list;
    }

    @Override
    public PacReslutLevel2 getDetails(PacReslutLevel2 rereslutLevel) {
        if(rereslutLevel.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        PacReslutLevel2 one = lambdaQuery().eq(PacReslutLevel2::getId, rereslutLevel.getId()).one();
        return one;
    }

    @Override
    public boolean add(PacReslutLevel2 rereslutLevel) {
        if(rereslutLevel.getResLevelId() == null){
            throw new RuntimeException("父类型id不能为空");
        }
        if(rereslutLevel.getResName() == null){
            throw new RuntimeException("名称不能为空");
        }
        return save(rereslutLevel);
    }

    @Override
    public boolean edit(PacReslutLevel2 rereslutLevel) {
        if(rereslutLevel.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        if(rereslutLevel.getResLevelId() == null){
            throw new RuntimeException("父类型id不能为空");
        }
        if(rereslutLevel.getResName() == null){
            throw new RuntimeException("名称不能为空");
        }
        return updateById(rereslutLevel);
    }

    @Override
    public boolean delete(PacReslutLevel2 rereslutLevel) {
        if(rereslutLevel.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        return removeById(rereslutLevel);
    }

    @Override
    public List<PacReslutLevel2> getAllList(PacReslutLevel2 rereslutLevel) {
        return lambdaQuery().eq(PacReslutLevel2::getResLevelId, rereslutLevel.getResLevelId()).list();
    }
}




